import defaultSettings from '@/settings'
import store from '@/store/index'

const default_title = defaultSettings.title

// 获取页面标题
export default async function getPageTitle(pageTitle, callback) {
  const pageTitleFromStore = store.getters.pagetitle || await fetchPageTitleFromStore();
  const title = pageTitleFromStore || default_title;

  const fullTitle = pageTitle ? `${pageTitle} - ${title}` : title;
  callback(fullTitle);
}

async function fetchPageTitleFromStore() {
  if (!store.getters.pagetitle) {
    const res = await store.dispatch('app/setPageTitle');
    return res || default_title;
  }
  return store.getters.pagetitle;
}
